﻿// The Redirector HttpModule Managed By a SharePoint List  - Attribution notice
// Author(s):
//
//      Stacy Draper (stacyDraper@WildWires.com, http://www.WildWires.com)
//
// This file is distributed under the terms of the Microsoft Community 
// License (Ms-CL). See http://www.codeplex.com/features/Project/License.aspx
// for the complete terms of use. 
// 
// For any distribution that contains code from this file, this notice of 
// attribution must remain intact, and a copy of the license must be 
// provided to the recipient.

using System;
using System.Web;
using System.Text;
using System.Xml;
using System.Configuration;
using Redirector.Lists; 

namespace WW.Redirect
{

    public class Redirector : IHttpModule
    {
        public Redirector()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        #region IHttpModule Members

        public void Dispose()
        {
            //
        }

        public void Init(HttpApplication application)
        {
            application.BeginRequest += (new EventHandler(this.Application_BeginRequest));
        }

        private void Application_BeginRequest(Object source, EventArgs e)
        {
            try
            {

                if (((HttpApplication)source).Context.Request.Url.ToString().Contains(ConfigurationSettings.AppSettings["GoKeyword"]))
                {
                    HttpApplication application = (HttpApplication)source;
                    HttpContext context = application.Context;

                    String url = context.Request.Url.ToString();
                    String[] urlPieces = url.Split('/');
                    for (int i = urlPieces.Length - 1; i >= 0; i--)
                    {
                        if (urlPieces[i] == ConfigurationManager.AppSettings["GoKeyword"].Replace("/", ""))
                        {
                            if (i + 1 <= urlPieces.Length)
                                context.Response.Redirect(getRedirectUrl(urlPieces[i + 1], new ConfigSettings()));
                            else
                                return;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //throw;
                //add your own brand of error handling here.
            }
        }
        #endregion

        public String getRedirectUrl(String lookupCode, WW.Redirect.Interfaces.IConfig config)
        {

            switch (config.Get("DataSource").ToLower())
            {
                case "sharepoint":
                    return (new GetRedirectUrlSP()).getRedirectUrl(lookupCode, config);
                    break;
                case "xml":
                    return (new GetRedirectUrlXml()).getRedirectUrl(lookupCode, config);
                    break;
                case "sql":
                    return (new GetRedirectUrlSql()).getRedirectUrl(lookupCode, config);
                    break;
                default:
                    return "";
                    break;
            }
        }

        private string getUrlFromHyperlinkColumn(String splink)
        {
            return splink.Split(',')[0];
        }
    }
    
}
